/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bl;

import Config.Localization;
import da.DAUser;
import entities.User;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author HungVM
 */
public class BLUser {
    
    DAUser daUser = new DAUser();
    public static int f;
    public boolean checkLogin(User user) throws Exception{
       if(user.getUserName().equals("")){
           return false;
        }else{
           if(daUser.checkLogin(user))
               return true;
           else
               return false;
        }
    }

    public boolean changePass(String user, String old,String newPass, String newPassConfirm){
        if(newPass.equalsIgnoreCase("") || old.equalsIgnoreCase("") || newPassConfirm.equalsIgnoreCase("")){
            JOptionPane.showMessageDialog(null, Localization.getMessage("ChangePassError1"));
            return false;
        }
        else if(!newPass.equals(newPassConfirm))
        {
            JOptionPane.showMessageDialog(null, Localization.getMessage("ChangePassError2"));
            return false;
        }
        else{
            if(!daUser.changePass_DAL(user, old, newPass, newPassConfirm)){
                JOptionPane.showMessageDialog(null, Localization.getMessage("ChangePassError3"), "Warring", JOptionPane.WARNING_MESSAGE);
                return false;
            }
            else return true;
         }
    }

    public ArrayList<User> getAll(){
        return daUser.getAll();
    }
    
    public ArrayList<User> getById(int id){
        return daUser.getById(id);
    }
    
    public ArrayList<User> getByUserName(String uName){
        return daUser.getByUserName(uName);
    }
    
    public ArrayList<User> getByFullName(String fName){
        return daUser.getByFullName(fName);
    }
    
    public ArrayList<User> getByIdentity(String identity){
        return daUser.getByIdentity(identity);
    }
    
    public ArrayList<User> getbyLock(int isLock){
        return daUser.getLock(isLock);
    }
            
    

    public boolean insertUser(User user){
        if(this.checkUser(user.getUserName())){
            f=1;
            return false;
        }
        else
            return daUser.insertUser(user);
    }
    
    public boolean updateUser(User user){
        return daUser.updateUser(user);
    }

    public boolean checkUser(String user){
        return daUser.checkUser(user);
    }
    
    public boolean checkIdentity(String identity){
        return daUser.checkIdentity(identity);
    }
    
    public boolean validateIdentity(String identity){
        Pattern pattern;
        Matcher matcher;
        String IDENTITY_PATTERN ="^[0-9]{9}$";
        pattern = Pattern.compile(IDENTITY_PATTERN);
        matcher = pattern.matcher(identity);
        if(!matcher.matches())
            JOptionPane.showMessageDialog(null, Localization.getMessage("REG2"),"Error",JOptionPane.WARNING_MESSAGE);
        return matcher.matches();
    }
    
    
    /* Phone Number formats: (nnn)nnn-nnnn; nnnnnnnnnn; nnn-nnn-nnnn 
#     ^\\(? : May start with an option "(" . 
#     (\\d{3}): Followed by 3 digits. 
#     \\)? : May have an optional ")" 
#     [- ]? : May have an optional "-" after the first 3 digits or after optional ) character. 
#     (\\d{3}) : Followed by 3 digits. 
#      [- ]? : May have another optional "-" after numeric digits. 
#      (\\d{4})$ : ends with four digits. 
#  
#          Examples: Matches following phone numbers: 
#          (123)456-7890, 123-456-7890, 1234567890, (123)-456-7890 
#  
# */  
    public boolean validatePhone(String phone){
        Pattern pattern;
        Matcher matcher;
        String PHONE_PATTERN ="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
        pattern = Pattern.compile(PHONE_PATTERN);
        matcher = pattern.matcher(phone);
        if(!matcher.matches())
            JOptionPane.showMessageDialog(null, Localization.getMessage("REG3"),"Error",JOptionPane.WARNING_MESSAGE);
        return matcher.matches();
    }
    
    public boolean validateEmail(String email){
        Pattern pattern;
        Matcher matcher;
        String EMAIL_PATTERN ="^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        pattern = Pattern.compile(EMAIL_PATTERN,Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(email);
        if(!matcher.matches())
            JOptionPane.showMessageDialog(null, Localization.getMessage("REG4"),"Error",JOptionPane.WARNING_MESSAGE);
        return matcher.matches();
    }
    
    public boolean validateUserName(String userName){
        Pattern pattern;
	Matcher matcher;
        String USERNAME_PATTERN = "^[A-Za-z0-9_-]{3,15}$";
        pattern = Pattern.compile(USERNAME_PATTERN);
        matcher = pattern.matcher(userName);
        if(!matcher.matches())
            JOptionPane.showMessageDialog(null, Localization.getMessage("REG1"), "Error", JOptionPane.WARNING_MESSAGE);
	return matcher.matches();
    }

    
    
    public boolean lockUser(int id){
        return daUser.lockUser(id);
    }
    
    public boolean unLockUser(int id){
        return daUser.unLockUser(id);
    }
    
    public boolean deleteUser(int id){
        return daUser.deleteUser(id);
    }

}
